home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 27 / MacFormat n. 27 (Spain) / Mac Format 26.bin / Shareware / Programación / Word Services XCMD 1.0d3 / GoToWebPage XCMD / SearchForApp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-18  |  4.6 KB  |  239 lines

  1. #include "SearchForApp.h"
  2.  
  3. OSErr SearchVolForBrowser( short vRef,
  4.                             Boolean *gotItPtr,
  5.                             OSType **creators,
  6.                             short numCreators,
  7.                             FSSpecPtr specPtr,
  8.                             OSType *creatorPtr )
  9. {
  10.     short    i;
  11.     DTPBRec    dPB;
  12.     short    refNum;
  13.     Str255    name;
  14.     FInfo    fndrInfo;
  15.     OSErr    err;
  16.     char     *cP;
  17.     
  18.     *gotItPtr = false;
  19.  
  20.     cP = (char*)&dPB;
  21.  
  22.     for ( i = 0; i < sizeof( dPB ); i++ ){
  23.         *cP++ = 0;
  24.     }
  25.     
  26.     dPB.ioNamePtr = (StringPtr)NULL;
  27.     dPB.ioVRefNum = vRef;
  28.     
  29.     err = PBDTGetPath( &dPB );
  30.     if ( err )
  31.         return err;
  32.     
  33.     refNum = dPB.ioDTRefNum;
  34.     
  35.     dPB.ioCompletion = (IOCompletionUPP)NULL;
  36.     dPB.ioNamePtr = name;
  37.     
  38.     for ( i = 0; i < numCreators; i++ ){
  39.         dPB.ioFileCreator = (*creators)[ i ];
  40.         
  41.         dPB.ioIndex = 1;
  42.         
  43.         do{
  44.             err = PBDTGetAPPL( &dPB, false );
  45.             if ( err == afpItemNotFound || err == paramErr)
  46.                 break;
  47.             if ( err )
  48.                 return err;
  49.             
  50.             // We got the application info, but it is possible that the application
  51.             // has been deleted without the desktop file being updated.  Check to
  52.             // see that the file is still there.
  53.             
  54.             err = HGetFInfo( vRef, dPB.ioAPPLParID, name, &fndrInfo );
  55.             if ( !err ){
  56.                 err = FSMakeFSSpec( vRef, dPB.ioAPPLParID, name, specPtr );
  57.                 if ( err )
  58.                     return err;
  59.                 
  60.                 *creatorPtr = dPB.ioFileCreator;
  61.  
  62.                 *gotItPtr = true;
  63.                 return noErr;
  64.             }
  65.         }while( !err );    
  66.     }
  67.     
  68.     return noErr;
  69. }
  70.  
  71. OSErr LaunchTheApp( FSSpecPtr specPtr )
  72. {
  73.     OSErr        err;
  74.     Boolean        changed;
  75.     AppleEvent        launchEvent;
  76.     AppleEvent        replyEvent;
  77.     AEDesc            aliasDesc;
  78.     AEDesc            folderDesc;
  79.     FSSpec            folderSpec;
  80.     AEDesc            finderAddr;
  81.     DescType        finderSig;
  82.     AliasHandle        fAliasHdl;
  83.     AliasHandle        appAliasHdl;
  84.     AEDescList        aliasList;
  85.  
  86.     // Launch the browser by sending an Open Selection apple event to the Finder.
  87.     // We need to do this so that the process shows up in the application menu, so that
  88.     // we see zoomrects on launch, and so that the application's icon is dimmed in its
  89.     // Finder window
  90.  
  91.     // Taken from Writeswell Jr.'s LaunchSpeller in DoChecking.c
  92.  
  93.     
  94.     /* make an alias for the parent folder */
  95.     
  96.     err = FSMakeFSSpec( specPtr->vRefNum,
  97.                         specPtr->parID,
  98.                         (StringPtr)NULL,
  99.                         &folderSpec );
  100.     if ( err ){
  101.         return err;
  102.     }
  103.     
  104.     err = NewAlias( (FSSpecPtr)NULL, &folderSpec, &fAliasHdl );
  105.     if ( err ){
  106.         return err;
  107.     }
  108.  
  109.     /* Create the event to send to the Finder */
  110.     
  111.     finderSig = 'MACS';                        /* Creator code of finder; type is 'FNDR' */
  112.     err = AECreateDesc( typeApplSignature,
  113.                         (Ptr)&finderSig,
  114.                         sizeof( finderSig ),
  115.                         &finderAddr );
  116.     if ( err ){
  117.         return err;
  118.     }    
  119.                 
  120.     err = AECreateAppleEvent( kAEFinderEvents,
  121.                                 kAEOpenSelection,
  122.                                 &finderAddr,
  123.                                 kAutoGenerateReturnID,
  124.                                 kAnyTransactionID,
  125.                                 &launchEvent );
  126.     
  127.     if ( err ){
  128.         return err;
  129.     }
  130.     err = AEDisposeDesc( &finderAddr );
  131.     if ( err ){
  132.         return err;
  133.     }
  134.     
  135.     /* Insert the folder alias record as the direct object of the batch event */
  136.  
  137.     /* First make a descriptor of the alias */
  138.     HLock( (Handle) fAliasHdl );
  139.  
  140.     err = AECreateDesc( typeAlias,
  141.                         (Ptr)*fAliasHdl,
  142.                         (*fAliasHdl)->aliasSize,
  143.                         &folderDesc );
  144.  
  145.     HUnlock( (Handle) fAliasHdl );
  146.     DisposHandle( (Handle) fAliasHdl );
  147.  
  148.     if ( err ){
  149.         return err;
  150.     }    
  151.     
  152.     err = AEPutParamDesc( &launchEvent,
  153.                             keyDirectObject,
  154.                             &folderDesc );
  155.     if ( err ){
  156.         return err;
  157.     }
  158.     err = AEDisposeDesc( &folderDesc );
  159.     if ( err ){
  160.         return err;
  161.     }
  162.  
  163.     /* Put the application alias on the event */
  164.     err = NewAlias( (FSSpecPtr)NULL, specPtr, &appAliasHdl );
  165.     if ( err ){
  166.         return err;
  167.     }
  168.  
  169.     HLock( (Handle) appAliasHdl );
  170.  
  171.     err = AECreateDesc( typeAlias,
  172.                         (Ptr)*appAliasHdl,
  173.                         (*appAliasHdl)->aliasSize,
  174.                         &aliasDesc );
  175.  
  176.     HUnlock( (Handle) appAliasHdl );
  177.     DisposeHandle( (Handle)appAliasHdl );
  178.  
  179.     if ( err ){
  180.         return err;
  181.     }    
  182.  
  183.     /* Now make a list of the alias descriptor.  There is only one element in this
  184.      * list
  185.      */
  186.     
  187.     err = AECreateList( (Ptr)NULL, (Size)0, false, &aliasList );
  188.     
  189.     if ( err ){
  190.         return err;
  191.     }
  192.     
  193.     err = AEPutDesc( &aliasList, 1L, &aliasDesc );
  194.     
  195.     if ( err ){
  196.         return err;
  197.     }
  198.  
  199.     err = AEDisposeDesc( &aliasDesc );
  200.     if ( err ){
  201.         return err;
  202.     }
  203.     
  204.     err = AEPutParamDesc( &launchEvent,
  205.                             keySelection,
  206.                             &aliasList );
  207.     if ( err ){
  208.         return err;
  209.     }
  210.  
  211.     err = AEDisposeDesc( &aliasList );
  212.     if ( err ){
  213.         return err;
  214.     }
  215.  
  216.     err = AESend( &launchEvent,
  217.                     &replyEvent,
  218.                     kAEWaitReply + kAECanInteract,        /* Don't allow later switch */
  219.                     kAENormalPriority,
  220.                     1800,                                // Thirty seconds; browser can take a long time to initialize
  221.                     (AEIdleUPP)NULL,
  222.                     (AEFilterUPP)NULL );
  223.     
  224.     if ( err ){
  225.         return err;
  226.     }
  227.     err = AEDisposeDesc( &launchEvent );
  228.     if ( err ){
  229.         return err;
  230.     }
  231.     
  232.     err = AEDisposeDesc( &replyEvent );
  233.     if ( err ){
  234.         return err;
  235.     }
  236.     
  237.     return noErr;
  238. }
  239.